/*
 * io_buf.h
 *
 *  Created on: 2012-7-5
 *      Author: ThinkPad
 */

#ifndef IO_BUF_H_
#define IO_BUF_H_

#include "smart_lock.h"
/*
*buf def
*/
static const size_t IO_CHUNK_SIZE =  0xffff;   /*default io block szie:0xffff*/
static const size_t DEF_CHUNK_COUNT   = 100;   /*default memory block count:20*/

struct buf_chunk
{
    int index;
    char *buffer;
    unsigned long size;
    unsigned long buf_len;
    unsigned long offset;

    uint64_t    foffset;
    buf_chunk * prev;
    buf_chunk * next;
};

/*all pool chunk*/
struct mem_chunk
{
    size_t len;
    char * mem;
};

class io_buf
{
public:
        io_buf(void);
        ~io_buf(void);

        int init_buf(int ichunck_count = DEF_CHUNK_COUNT,int ichunk_size = IO_CHUNK_SIZE);
        int alloc_buf(buf_chunk **pbuf);
        int free_buf(buf_chunk *pbuf);
        int get_count(void);
        int get_free_count(void);
private:
        int add_memory(void);
        int buf_block_size;
        int buf_block_count;
        int total_count;
        int free_count;
        stack<buf_chunk *> buf_pool;
        stack<mem_chunk *> mem_pool;
        smart_lock   buf_lock;
};

#endif /* IO_BUF_H_ */
